<?php
    require('config/config.php');
    include('header.php');
    
    $services = Service::rechercheTout();
    $typeMachines = TypeMachine::rechercheTout();
    $typeEvenements = TypeEvenement::rechercheTout();
    
    if(\Utils\Outils::getValue("envoie")) {
        $service = Service::recherche(intval(\Utils\Outils::getValue("service")));
        $typeMachine = TypeMachine::recherche(intval(\Utils\Outils::getValue("typeMachine")));
        if(Utils\Pattern::isAdresseIPV4(\Utils\Outils::getValue("adresseIP"))) {
            $machine = new \Machine(0, \Utils\Outils::getValue("nom"), \Utils\Outils::getValue("adresseIP"), 
                                    \Utils\Outils::getValue("position"), \Utils\Outils::getValue("community"), new DateTime(), 
                                    $typeMachine, $service);
            $machine->ajout();
            foreach(\Utils\Outils::getValue("typeEvenements") as $te) {
                $typeEvenement = TypeEvenement::recherche(intval($te));
                $restreindre = new \Restreindre(0, $typeEvenement, $machine);
                $restreindre->ajout();
            }
            echo    'Machine ajoutée avec succès';
        }
        else {
            echo    'Adresse IP incorrecte';
        }
    }
        
?>
    <!-- Header -->
    <header class="container_12 " id="header">
        <div class="grid_3 alpha">
            <img src="./stylesheets/images/logo1.png" alt="Logo"/>
        </div>
<?php
    require 'tableauCriticite.php';
?>
    </header>
    <!-- End Header -->
     <div class="clear"></div>
    <section class="content">
<?php
    require 'menu.php';
?>
<form class="compte" method="POST" action="ajoutMachine.php">
    <fieldset>
        <legend>Ajouter un nouveau poste</legend><br>
    <p>
        <label class="grid_4" for="nom">Nom  de la machine:</label>
        <input class="grid_4" type="text" name="nom" id="nom">
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="adresseIP">Adresse IP :</label>
        <input class="grid_4" type="text" name="adresseIP" id="adresseIP">
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="position">Position physique :</label>
        <input class="grid_4" type="text" name="identifiant" id="position">
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="community">Community SNMP :</label>
        <input class="grid_4" type="text" name="community" id="community">
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="service"> Service :</label>
        <select name="service" id="service">
<?php
    foreach ($services as $service) {
        echo    '<option value="'.$service->getId().'">'.$service.'</option>';
    }
?>
        </select>
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="typeMachine"> Type de machine :</label>
        <select name="typeMachine" id="typeMachine">
<?php
    foreach ($typeMachines as $typeMachine) {
        echo    '<option value="'.$typeMachine->getId().'">'.$typeMachine.'</option>';
    }
?>
        </select>
    </p>
    <div class="clear"></div>
    <p>
        <label class="grid_4" for="typeEvenements"> Type d'évenements liés :</label> 
        <select name="typeEvenements[]" id="typeEvenements" multiple style="height:50px;">
<?php
    foreach ($typeEvenements as $typeEvenement) {
        echo    '<option value="'.$typeEvenement->getId().'">'.$typeEvenement.'</option>';
    }
?>
        </select>
    </p>
    <div class="clear"></div>
    </fieldset>
    <input type="submit" name="envoie" value="Ajouter"/>
    
</form> 
</section>
